<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">
    $(document).ready(function() {   

        //   StatusUtil.getSysStatus();
        Page.render('#container', '#index_template', DOC);

        function getHomeInfo() {
            var sysStatus = null; 
            Page.postJSON({
                json: {
                    // asynss:true,
                    cmd: RequestCmd.GET_HOME_INFO
                },
                success: function(datas) {
                    
                    var datas = datas.data;
                    var timeTo = new Date(parseInt(datas.systime)*1000).toLocaleString();
                    var time = timetrans(datas.systime);
                    $('#sysTime').text(time);
                    var timestamp = new Date(datas.systime1).getTime();
                    var timestamp = (new Date()).getTime();
                    var systime = datas.systime;
                    var intervalId2 = setInterval(function () {
                        var time = timetrans(systime);
                        $('#sysTime').text(time);
                        systime ++;
                    },1000)

                    sysStatus = datas.sys_status;
                    
                    Page.isShowLangu = datas.web_language_select_enable;
                    if(datas.web_language_select_enable == "1"){
                        $("#languageSelect").show();
                        $(".tr_language").show();
                    }else{
                        $("#languageSelect").hide();
                        $(".tr_language").hide();
                    }            
               
                    var wifiStatus = sysStatus.wifi;
                    var interStatus = sysStatus.ethInter;
                    $('#wifiInfo').html(StatusUtil.formatWiFiInfo(wifiStatus.status));
                    $('#wifiInfo5g').html(StatusUtil.format5gWiFiInfo(wifiStatus.status5));
                    $('#simInfo').html(StatusUtil.formatSimInfo(sysStatus.is_sim_exist));
                    for(var i=0;i<interStatus.length;i++){
                        if(i< 3){
                            var j=i+1;
                            $('#lan'+j).html(StatusUtil.formatNetInterfaceInfo(interStatus[i], "Lan"+j));
                            if(interStatus[i] == "0"){
                                $('#lan'+j).hide(0);
                            }
                        }else if(i == 3){
                            $('#wan1').html(StatusUtil.formatNetInterfaceInfo(interStatus[3], "Lan4/Wan"));
                            if(interStatus[3] == "0"){
                                $('#wan1').hide(0);
                            }
                        }
                    }
                    sessionStorage["2g_register_status"] = sysStatus["2g_register_status"];
                    sessionStorage["3g_register_status"] = sysStatus["3g_register_status"];
                    sessionStorage["4g_register_status"] = sysStatus["4g_register_status"];
                    sessionStorage["rsrp"] = sysStatus.rsrp;
                    sessionStorage["signal_lvl"] = sysStatus.signal_lvl;
                    sessionStorage["wifi_status"] = wifiStatus.status;
                    sessionStorage["wifi5_status"] = wifiStatus.status5;
                    sessionStorage["network_link_stauts"] = sysStatus.network_link_stauts;
                    if(sysStatus.card_status == 0) {
                        var singalLevel = sysStatus.signal_lvl;
                        var netInfo = StatusUtil.formatSingalLevel(singalLevel);
                        $('#netInfo').html(netInfo);
                    }else{
                        var netInfo = StatusUtil.formatSingalLevel(0);
                        $('#netInfo').html(netInfo);
                    }
                    if(datas.sim_card_status == '1'){
                        $("#simPInPosition").text(DOC.title.simPInPosition);
                    }

                }
            })
        };
        getHomeInfo();

        var timeout = null;
        if(timeout) {
            clearTimeout(timeout);
            timeout = null
        }
        timeout = setTimeout(StatusUtil.getSysStatus, 10000);
        
        var Pageauth = Page.AUTH;
        var Infoauth = parseInt(Pageauth.substr(Pageauth.length-2),16).toString(2);
        var Settingauth =  parseInt(Pageauth.substr(Pageauth.length-4,2),16).toString(2);
        var Sysauth = parseInt(Pageauth.substr(Pageauth.length-6,2),16).toString(2);
        var menus = [];


        var infoMenu = new MenuHead("info_menu", MENU.info.head);

        // infoMenu.push(new MenuBody("HOME",  MENU.info.home));
        infoMenu.push(new MenuBody("FourG_INFO", MENU.info.mobile));
        infoMenu.push(new MenuBody("WAN_INFO",  MENU.info.wan));
        infoMenu.push(new MenuBody("LAN_INFO",  MENU.info.lan));       
        infoMenu.push(new MenuBody("WIFI_INFO", MENU.info.wifi));
        infoMenu.push(new MenuBody("DEVICE_INFO", MENU.info.version));
        menus.push(infoMenu);
        var settingMenu = new MenuHead("setting_menu", MENU.setting.head);
        settingMenu.push(new MenuBody("NETWORK_CONFIG", MENU.setting.dhcp));
        settingMenu.push(new MenuBody("WIRELESS_CONFIG", MENU.settingWifi.wifi));
        settingMenu.push(new MenuBody("WLAN_5G_CONFIG", MENU.settingWifi.wifi5g));
        if(Page.getShowHideByBit(3))
            settingMenu.push(new MenuBody("APN_CONFIG", DOC.apn.setting));
        settingMenu.push(new MenuBody("SIP_CONFIG", MENU.setting.sip))
        if(Page.getShowHideByBit(16))
            settingMenu.push(new MenuBody("WAN_SETTINGS", MENU.setting.wan));
        if(Page.getShowHideByBit(15))
            settingMenu.push(new MenuBody("ROUTE_SETTING", TAB.advance.route));
        if(Page.getShowHideByBit(14))
            settingMenu.push(new MenuBody("TR069_SETTING", TAB.advance.tr069));
        // settingMenu.push(new MenuBody("QUICK_SETTINGS", MENU.setting.quick));
        if(Page.getShowHideByBit(7))
            settingMenu.push(new MenuBody("ADVANCED_CONFIG", MENU.setting.advanced));
        menus.push(settingMenu);

        
        // FW BEGIN 
        var fwMenu = new MenuHead("fw_menu", MENU.fw.head);
        // fwMenu.push(new MenuBody("FE_DEFAULT", TAB.fw.defaultSetting));
        fwMenu.push(new MenuBody("FW_RULE", MENU.fw.filters));
        // fwMenu.push(new MenuBody("FW_MAC_FILTER", MENU.fw.macFilter));
        fwMenu.push(new MenuBody("FW_PORT_MAPPING", MENU.fw.portMapping));
        fwMenu.push(new MenuBody("FW_URL_FILTER", MENU.fw.urlFilter));
        fwMenu.push(new MenuBody("FW_IP_MAC_BINDING", MENU.fw.ipMacBinding));
        fwMenu.push(new MenuBody("FW_ACL_FILTER", MENU.fw.aclFilter));
        fwMenu.push(new MenuBody("FW_SPEED_LIMIT", MENU.fw.speedLimit));
        fwMenu.push(new MenuBody("FW_DMZ", "DMZ"));
        fwMenu.push(new MenuBody("STATIC_ARP_BINDING", MENU.sys.staticArp));
        if(Page.getShowHideByBit(21))
            fwMenu.push(new MenuBody("FW_UPNP", MENU.fw.upnp));


        menus.push(fwMenu);
         // FW END 
        
        var sysMenu = new MenuHead("manage_menu", MENU.sys.manage);
        sysMenu.push(new MenuBody("SYS_SET", MENU.sys.settings));
        sysMenu.push(new MenuBody("SYS_LOG", MENU.sys.sysLog));
        sysMenu.push(new MenuBody("SYS_UPDATE", MENU.sys.firmwareUpdate));
        sysMenu.push(new MenuBody("CONFIG_UPDATE", MENU.sys.configUpdate));
        sysMenu.push(new MenuBody("SYS_TIME", MENU.sys.timeSetting));
        if(Page.getShowHideByBit(5))
            sysMenu.push(new MenuBody("NETWORK_TOOL", MENU.sys.networkTools));
        if(Page.getShowHideByBit(6))  
            sysMenu.push(new MenuBody("SYS_AT", MENU.sys.at));
            
        sysMenu.push(new MenuBody("SYS_REBOOT", MENU.sys.reboot));
            
        menus.push(sysMenu);
        
            
        
        $('#menu').html(MenuUtil.create(menus));

        /* LANGUAGE BEGIN */
        var $languageSelect = $('#languageSelect');
        $languageSelect.html(Page.getLanguageList(Page.language)).change(function() {
            Page.changeLanguage($languageSelect);
        });
        //  Page.getLanguageConfig();
        /* LANGUAGE END */

        function setMenuItem(itemId) {
            var $lis = $('#menu li');
            $lis.removeClass('current');
            $('#' + itemId).parent().addClass('current');
        }

        $('#menu').accordion({
            header: 'h3',
            autoheight: false,
            duration: 150
        });
        $('#menu h3').hover(function() {
            $(this).not('.selected').addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });

        Page.$iframe = $('#right');
        Page.initPage();
        $(window).resize(function(){
            Page.initPage();
        });

        $('#logout').click(function() {
            Page.postJSON({
                json: {
                    cmd: RequestCmd.LOGOUT,
                    method: JSONMethod.POST
                },
                success: function(data) {
                  sessionStorage['sessionId'] = '';
                  sessionStorage['loginName'] = '';
                  sessionStorage['auth'] = '';
                  sessionStorage['level'] ='';
                  location.href = Page.getUrl(Url.LOGIN);
                }
            });

            
        });

        var apnGuideClick = false;
        $('#menu h3').bind('click', function(e){
            //if (!apnGuideClick && Page.apnGuide) {
              //  apnGuideClick = true;
               // $('#BASIC_CONFIG').click();
           // } else {
            //    $(this).next().find("a").eq(0).click();
           // }
           
            $(this).next().find("a").eq(0).click();
        });

        $('#menu li a').bind('click', function(e) {
            var id = this.id;
            var item = MenuItem[id];
            if (item != null) {
                if (item.cmd == RequestCmd.SYS_REBOOT) {
                    SysUtil.rebootDevice(PROMPT.confirm.reboot);
                } else {
                    setMenuItem(id);
                    Page.menuItem = item;
                    Page.$iframe.load(item.url);
                    
                }
            }
        });

        $('#smsInfo').click(function() {
            var htmlVal = $(this).html();
            if( htmlVal.indexOf("newsms") > -1) {
                $('#SMS_RECEIVE').click();
            } else {
                return false;
            }

        });

        $('#onlineHelp').click(function() {
            $('#SYS_HELPER').click();
        });

        $('#btnClose').click(function() {
            $('#info_box').hide();
            $('#mask').hide();
            $('#progress_box').hide();
        });
        
        // function listenOperation () {
        //     var maxTime = 20; // seconds
        //     var time = maxTime;
        //     $('body').on('keydown mousemove mousedown', function(e){
        //         time = maxTime; // reset
        //     });
        //     var intervalId = setInterval(function(){
        //         time--;
        //         if(time <= 0) {
        //             clearInterval(intervalId);
        //             $('#logout').click();
        //         }
        //     }, 1000*60);

        // }

            function loginSessionTimeout() {
                // timeout的单位是分钟
                var timeout = parseFloat(sessionStorage['timeout']),
                time = timeout ? timeout : 20;
                timer = null;
                if(timer) {
                    clearTimeout(timer);
                    timer = null;
                }
                timer = setTimeout(function() {
                    $('#logout').click();
                }, time * 60 * 1000) 
                $('body').on('keydown mousemove mousedown', function(){
                    time = timeout ? timeout : 20; // reset
                    clearTimeout(timer);
                    timer = setTimeout(function() {
                        $('#logout').click();
                    }, time  * 60 * 1000) 
                });    
                    
            };
           

        // function showSysTime() {
        //     Page.postJSON({
        //         json: {
        //             cmd: RequestCmd.GET_SYSTEM_TIME
        //         },
        //         success: function(data) {
        //           var timeTo = new Date(parseInt(data.systime)*1000).toLocaleString();
        //           var time = timetrans(data.systime);
        //            $('#sysTime').text(time);
        //          var timestamp = new Date(data.systime1).getTime();
        //         var timestamp = (new Date()).getTime();
        //           var systime = data.systime;
        //             var intervalId2 = setInterval(function () {
        //                 var time = timetrans(systime);
        //                 $('#sysTime').text(time);
        //                 systime ++;
        //             },1000)
        //         }
        //     });
        // }
        
        function timetrans(date){
            var date = new Date(date*1000);//如果date为13位不需要乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
            var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds()) + ' ';
            // var a = new Array(DOC.week.item, DOC.week.item1, DOC.week.item2, DOC.week.item3, DOC.week.item4, DOC.week.item5, DOC.week.item6);
   //          var w = a[date.getDay()];
            return Y+M+D+h+m+s;
        }
        
        // 页面轮询
        window.timeoutStart4G = false;
        window.timeoutStartWan = false;
        window.time4g = null;
        window.timeWan = null;
        $('#menu').click(function() {
            if(window.timeoutStart4G || window.timeoutStartWan) {
                clearInterval(window.time4g);
                clearInterval(window.timeWan);
                return;
            }else {
                return;
            }
        });
        $('#FourG_INFO').click(function() {
            window.timeoutStart4G = true;
            window.timeoutStartWan = false;
        });
        $('#WAN_INFO').click(function() {
            window.timeoutStartWan = true;
            window.timeoutStart4G = false;
        });  

        $('#menu h3').eq(0).next().find("a").eq(0).click();
        $('#container').show();
        // showSysTime();
        // listenOperation ();
        loginSessionTimeout();
		
    });
	
	function noImage(img){ 
				img.style.display="none"; 
	}
</script>
<script type="text/template" id="index_template">
    <div id="header">
	<div id="logoDiv"><img src=".././images/customer.gif" onerror="noImage(this)" ></div>
        <div id="closeInternet"></div>
        <div id="userinfo">
            <!-- LANGUAGE -->
            <select id="languageSelect" name="languageSelect" style="display:none;"></select>
            <a id="logout" href="javascript:;"><%- link.logout %></a>&nbsp;
            <!-- HELP -->
            <a id="onlineHelp" href="javascript:;" style="display:none;"><%- link.help %></a>
        </div>
        <div id="timeinfo">
		 <label id="sysTime"></label>
		 </div>
        <div id="header_right"></div>
        <div id="siginfo">
            <table cellpadding="0" cellspacing="4">
                <tr>
                    <td id="simInfo"></td>
					<td id="netInfo"></td>
                    <td id="wifiInfo"></td>
                    <td id="wifiInfo5g"></td>
                    <td id="lteUsbInfo" style="display:none;"></td>
                    <td id="lan1" ></td>
                    <td id="lan2" ></td>
                    <td id="lan3" ></td>
                    <td id="lan4" style="display:none;"></td>
                    <td id="wan1" ></td>
                    <!-- SMS_INFO -->
                    <td id="smsInfo"></td>
                </tr>
            </table>

            
        </div>
        <div id="simPInPosition">
        </div>
    </div>

    <div id="main">
        <div id="left">
            <div id="left_t"></div>
            <div id="left_m">
                <div id="menu"></div>
            </div>
            <div id="left_b"></div>
        </div>

        <div id="right"></div>
    </div>

    <div id="footer">
        <span id="spanFooter"><%- copyright %></span>
    </div>

    <div id="mask"></div>

    <div id="info_box">
        <div id="info_head"></div>
        <div id="info_close"><input id="btnClose" type="button" value="<%- btn.close %>" /></div>
        <div id="info_tab_head"></div>
        <div id="info_body">
            <div id="info_main"></div>
        </div>
    </div>


    <div id="progress_box">
        <div id="progress_info"></div>
        <div id="progress_bar">
            <div id="progress_status"></div>
        </div>
    </div>
    <div id="edit_box"></div>
</script>
